package com.atyuntuku.yuntuku.controller.user;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.atyuntuku.yuntuku.annotation.AuthCheck;
import com.atyuntuku.yuntuku.common.BaseResponse;
import com.atyuntuku.yuntuku.common.ResultUtils;
import com.atyuntuku.yuntuku.constant.UserConstant;
import com.atyuntuku.yuntuku.controller.request.UserAddRequest;
import com.atyuntuku.yuntuku.controller.request.UserQueryRequest;
import com.atyuntuku.yuntuku.controller.request.UserRegisterRequest;
import com.atyuntuku.yuntuku.exception.ErrorCode;
import com.atyuntuku.yuntuku.exception.ThrowUtils;
import com.atyuntuku.yuntuku.pojo.User;
import com.atyuntuku.yuntuku.service.UserService;
import com.atyuntuku.yuntuku.utils.CommonUtils;
import com.atyuntuku.yuntuku.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * ClassName: UserController
 * Package: com.atyuntuku.yuntuku.controller
 * Description:
 *
 * @Author qinglong wei
 * @Create 2025/10/5 11:21
 * @Version 1.0
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @ApiOperation("注册")
    @PostMapping("/register")
    public BaseResponse<Boolean> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        return userService.userRegister(userRegisterRequest);
    }

    @GetMapping("/login")
    public BaseResponse<UserVO> userLogin(@RequestParam String userAccount, @RequestParam String userPassword, HttpServletRequest request) {
        return userService.userlogin(userAccount, userPassword, request);
    }

    @PostMapping("/logout")
    public BaseResponse<Boolean> userlogout(HttpServletRequest request) {
        return userService.userlogout(request);
    }

    @PostMapping("/useradd")
    @AuthCheck(mustAuth = UserConstant.ADMIN)
    public BaseResponse<Boolean> useradd(@RequestBody UserAddRequest userAddRequest)
    {
        return userService.addUser(userAddRequest);
    }

    //普通用户使用
    @GetMapping("/getuserbyid")
    public BaseResponse<UserVO> getUserByid(@RequestParam long id)
    {
        ThrowUtils.throwIf(id<=0, ErrorCode.PARAMS_ERROR,"param is valid，again!");
        return userService.getUserById(id);
    }

    @PostMapping("/getuserlist")
    @AuthCheck(mustAuth = UserConstant.ADMIN)
    public JSONObject getUserList(@RequestBody UserQueryRequest userQueryRequest)
    {
        return userService.getUserListPage(userQueryRequest);
    }


    @GetMapping("/getcurrentuser")
    public BaseResponse<UserVO> getCurrentUser(HttpServletRequest request)
    {
        User currentUser = CommonUtils.getCurrentUser(request);
        ThrowUtils.throwIf(ObjectUtil.isEmpty(currentUser),ErrorCode.NOT_LOGIN_ERROR,"请重新登录");
        UserVO userVO = new UserVO();
        BeanUtil.copyProperties(currentUser,userVO);
        return  ResultUtils.success(userVO);
    }
}
